<template>
	<view class="page_container">
		<CustomNavBar v-bind="reloadProps"></CustomNavBar>
		<view class="tabview">
			<up-subsection :list="list" :duration="100" :current="0" mode="subsection" font-size="16"
				@change="change"></up-subsection>
		</view>
		<view class="list_container">
			<up-list @scrolltolower="throttle(scrolltolower, 1000, true)" :pagingEnabled="true" :height="winHeight - 156">
				<up-list-item v-for="(item, index) in indexList" :key="index">
					<TradeInfoCard v-bind="item" />
				</up-list-item>
			</up-list>
		</view>
		<view>
			<OfferWin v-if="isShowOfferWin" v-bind="winProps" />
		</view>
	</view>
</template>

<script setup lang="ts">
import { http } from '@/utils/http';
import type { OfferWinProps, TradeInfoProps, ResTradeInfoData, ResTradeInfoList, CustomNavBarProps, LoginReturnData } from '@/utils/page_types';
import { ref, reactive, onBeforeMount, onMounted } from 'vue';
import throttle from 'uview-plus/libs/function/throttle'
import { onLoad } from '@dcloudio/uni-app';
import { useCurrentServe } from '@/stores/models/server';
import wechatLogin from '@/utils/wechatLogin';
import { useMemberStore } from '@/stores/models/menbers';
const isShowOfferWin = ref(false)
const indexList = ref<TradeInfoProps[]>([]);
const list = ref(['出售', '求购']);
const currentPage = ref(1)
const winProps = ref<OfferWinProps>({} as OfferWinProps)
const winHeight = ref();
const listType = ref(1);
const serverStore = useCurrentServe();
const searchWord = ref((''))
const memberStore = useMemberStore()
const reload = () => {
	indexList.value.length = 0;
	currentPage.value = 1;
	loadTradeInfo();
}

const loadByKeyWord = async (keyword: string) => {
	indexList.value.length = 0;
	currentPage.value = 1;
	const data = await http<Array<ResTradeInfoData>>({
		url: "/informationByKey",
		data: {
			type: listType.value,
			page: currentPage.value,
			size: 20,
			serverId: serverStore.serverId,
			keyword: keyword
		},
		method: "GET"
	});
	console.log("data", data)
	const list = data.data;
	for (let i = 0; i < list.length; i++) {
		let infoData = list[i];
		let obj: TradeInfoProps = {
			id: infoData.sdInfoId.toString(),
			title: infoData.title,
			infoOpenid: infoData.openId,
			userName: infoData.brand,
			updateTime: String((new Date(infoData.updateAt)).getTime()),
			price: infoData.price.toString(),
			num: infoData.num.toString(),
			onClick: onOffer
		}
		indexList.value.push(obj)
	}
	currentPage.value++;
}
const reloadProps = {
	reload: reload,
	onSearch: loadByKeyWord
}

onLoad(() => {
	winHeight.value = uni.getSystemInfoSync().windowHeight;
	console.log("winHeight", winHeight.value)
})


onMounted(async () => {
	if (!memberStore.profile) {
		try {
			let logoinResult: any = await wechatLogin();
			if (logoinResult.data.code == 1000) {
				let returnData = logoinResult.data.data as LoginReturnData;
				memberStore.setProfile(returnData);
			}
			console.log("登录成功：", logoinResult);
		} catch (e) {
			console.log("登录失败：", e);
		}
	}

	loadTradeInfo();
})

const scrolltolower = () => {
	loadTradeInfo();
};



const loadTradeInfo = async () => {
	console.log("loadTradeInfo", serverStore.serverId)
	const data = await http<Array<ResTradeInfoData>>({
		url: "/information",
		data: {
			type: listType.value,
			page: currentPage.value,
			size: 20,
			serverId: serverStore.serverId
		},
		method: "GET"
	});
	console.log("data", data)
	const list = data.data;
	for (let i = 0; i < list.length; i++) {
		let infoData = list[i];
		let obj: TradeInfoProps = {
			id: infoData.sdInfoId.toString(),
			title: infoData.title,
			infoOpenid: infoData.openId,
			userName: infoData.brand,
			updateTime: String((new Date(infoData.updateAt)).getTime()),
			price: infoData.price.toString(),
			num: infoData.num.toString(),
			onClick: onOffer
		}
		indexList.value.push(obj)
	}
	currentPage.value++;
}




const onWinClose = () => {
	isShowOfferWin.value = false;
};
const onWinOpen = () => {
	isShowOfferWin.value = true;
}



const onOffer = (infoId: string, infoOpenid: string) => {
	if (isShowOfferWin.value) {
		return
	} else {
		isShowOfferWin.value = true;
		winProps.value!.infoId = infoId
		winProps.value!.infoOpenId = infoOpenid
		winProps.value!.type = listType.value
		winProps.value.onClose = onWinClose
		winProps.value.onOpen = onWinOpen

	}
	console.log("infoID", infoId);
}


const change = (tabIndex: number) => {
	console.log('tabIndex ', tabIndex);

	if (listType.value == tabIndex) {
		indexList.value.length = 0;
		tabIndex === 1 ? listType.value = 0 : listType.value = 1;
		currentPage.value = 1
		loadTradeInfo();
	} else {
		tabIndex === 1 ? listType.value = 0 : listType.value = 1;
	}

}

</script>

<style lang="scss">
.page_container {
	margin-bottom: 56px;
}

.list_container {
	display: flex;
	flex-direction: column;
	align-items: center;
}

page {
	background-color: #f6f6f6;

}

.tabview {
	width: 95%;
	margin: 0 auto;
	margin-top: 3px
}
</style>
